﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Qreed.Diagnostics;
using Qreed.LoremIpsum;
using System.Reflection;
using Qreed.Reflection;
using System.Collections;
using System.Diagnostics;
using System.Threading;

namespace LoggerDemo
{
    public partial class listBoxLoggerForm : Form
    {
        BackgroundWorker _worker = new BackgroundWorker();
        
        public listBoxLoggerForm()
        {
            InitializeComponent();

            EnumHelper.DataBind(typeof(LogLevel), cbLogLevel, this, "LogLevel");

           

            chkDisplayFunctionName.DataBindings.Add("Checked", this, "IncludeFunctionName");
            //chkTimerEnabled.DataBindings.Add("Checked", _worker.IsBusy, "Enabled");

            _worker.WorkerSupportsCancellation = true;
            _worker.DoWork += new DoWorkEventHandler(_worker_DoWork);
            _worker.RunWorkerAsync();
        }

       

        void _worker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!_worker.CancellationPending)
            {
                string message = Generator.CreateLine(6, 12, GeneratorOptions.AppendLinePoints | GeneratorOptions.CapitalizeLines);

                LogLevel logLevel = (LogLevel)_rnd.Next(4);

                if (logLevel == LogLevel.Error)
                {
                    TestLogger.Write(message, new Exception(Generator.CreateLine(30, 50, GeneratorOptions.AppendLinePoints | GeneratorOptions.CapitalizeLines)));
                }
                else
                {
                    if ((_rnd.Next() & 1) == 1)
                    {
                        TestLogger.Write(message, logLevel);
                    }
                    else
                    {
                        string details = Generator.CreateLine(3, 6, GeneratorOptions.AppendLinePoints | GeneratorOptions.CapitalizeLines) +
                                         Environment.NewLine +
                                         Generator.CreateLine(3, 6, GeneratorOptions.AppendLinePoints | GeneratorOptions.CapitalizeLines) +
                                         Environment.NewLine +
                                         Generator.CreateLine(3, 6, GeneratorOptions.AppendLinePoints | GeneratorOptions.CapitalizeLines);

                      //  Trace.Write(message);

                        TestLogger.Write(message, details, logLevel);
                    }
                }

                Thread.Sleep(1000);
            }
        }

        [Browsable(true)]
        [Category("Logging")]
        public LogLevel LogLevel
        {
            get { return TestLogger.Level; }
            set { TestLogger.Level = value; }
        }

        [Browsable(true)]
        [Category("Logging")]
        public bool IncludeFunctionName
        {
            get { return TestLogger.IncludeStackFrame; }
            set { TestLogger.IncludeStackFrame = value; }
        }

        private Random _rnd = new Random();

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            TestLogger.IncludeStackFrame = chkDisplayFunctionName.Checked;
        }

        private void chkTimerEnabled_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTimerEnabled.Checked)
            {
                _worker.RunWorkerAsync();
            }
            else
            {
                _worker.CancelAsync();
            }
        }
    }
}
